home *** CD-ROM | disk | FTP | other *** search
- /********************************************************************
- FILENAME: VIEW_IO.CPP
- AUTHOR : JAKE HILL
- DATE : 12/1/94
-
- Copyright (c) 1994 by Jake Hill:
- If you use any part of this code in your own project, please credit
- me in your documentation and source code. Thanks.
- ********************************************************************/
-
- #define C
-
- #include "view.h"
- #include "trig.h"
-
- #ifdef __GNUC__
- #include <osbind.h>
- #include <unistd.h>
- #else
- #include <tos.h>
- #endif
-
- #include <stdio.h>
- #include <string.h>
- #include <process.h>
- #include <stdlib.h>
-
- /* Some more yucky global variables. */
- short NumThings;
- short NumSegs;
- short NumSides;
- short NumLines;
- short NumNodes;
- short NumSectors;
- short NumSSectors;
- short NumVertexes;
- short BlockmapSize;
-
- void SetView(short *, short *, short *, unsigned short *);
- void LoadThings(Directory_Entry *Dir, FILE *handle);
- void LoadSegs(Directory_Entry *Dir, FILE *handle);
- void LoadSides(Directory_Entry *Dir, FILE *handle);
- void LoadLines(Directory_Entry *Dir, FILE *handle);
- void LoadNodes(Directory_Entry *Dir, FILE *handle);
- void LoadSectors(Directory_Entry *Dir, FILE *handle);
- void LoadVertexes(Directory_Entry *Dir, FILE *handle);
- void LoadSSectors(Directory_Entry *Dir, FILE *handle);
-
- #define fix_w(X) (X) = ((((X) & 0xff) << 8) + (((X) >> 8) & 0xff))
-
- #if 0
- void fix_w(short &pcword)
- {
- short tmp;
-
- tmp = pcword;
- pcword = ((tmp & 0xff) << 8) + ((tmp >> 8) & 0xff);
- }
- #endif
-
- void fix_l(long *pclong)
- {
- long tmp;
-
- tmp = *pclong;
- *pclong = ((tmp & 0xff) << 24) + (((tmp >> 8) & 0xff) << 16) +
- (((tmp >> 16) & 0xff) << 8) + ((tmp >> 24) & 0xff);
- }
-
- /* Sets the graphics mode. */
- void setmode(unsigned short n)
- {
- }
-
- /* This function opens the wadfile, determines if it is a PWAD
- * or an IWAD, then reads in the resources for THINGS, LINES, SIDES,
- * VERTEXES, SEGS, SSECTORS, NODES, and SECTORS.
- */
-
- #ifndef C
- void View::OpenWad(char *WadName, long Level)
- #else
- void OpenWad(char *WadName, long Level)
- #endif
- {
- FILE *file, *handle;
- short EntriesRead = 0;
- unsigned long EntryOffset = 0;
-
- WAD_Header HEADER;
- Directory_Entry DIRECTORY;
-
- if ((file = fopen(WadName, "rb")) == NULL) {
- printf("ERROR:Could not open file %s.\n",WadName);
- exit(-1);
- }
-
- #ifdef __GNUC__
- file->_flag |= _IOBIN; /* The GNU libraries don't like "rb" */
- #endif
-
- fread(&HEADER, sizeof(WAD_Header), 1L, file);
-
- fix_l(&HEADER.num_entries);
- fix_l(&HEADER.foffset);
-
- if (strnicmp(HEADER.signature, "IWAD", 4) == 0) {
- printf("Reading IWAD file.\n");
- /* We need to skip past the PLAYPAL, and DEMO stuff if in an IWAD. */
- HEADER.foffset += 6L * sizeof(Directory_Entry);
- } else if (strnicmp(HEADER.signature, "PWAD", 4) == 0) {
- printf("Reading PWAD file.\n");
- } else {
- printf("This is not a valid WAD file!\n");
- exit(-1);
- }
-
- printf("This file contains %d directory entries.\n\n", HEADER.num_entries);
- EntryOffset += HEADER.foffset + (Level * 11L * sizeof(Directory_Entry));
-
- handle = file;
- while (EntriesRead < 11) {
- fseek(handle, EntryOffset, SEEK_SET);
- fread(&DIRECTORY, sizeof(Directory_Entry), 1L, file);
-
- fix_l(&DIRECTORY.foffset);
- fix_l(&DIRECTORY.size);
-
- if (strnicmp(DIRECTORY.name, "THINGS", 6) == 0)
- LoadThings(&DIRECTORY, handle);
- else if (strnicmp(DIRECTORY.name, "LINEDEFS", 8) == 0)
- LoadLines(&DIRECTORY, handle);
- else if (strnicmp(DIRECTORY.name, "SIDEDEFS", 8) == 0)
- LoadSides(&DIRECTORY, handle);
- else if (strnicmp(DIRECTORY.name, "VERTEXES", 8) == 0)
- LoadVertexes(&DIRECTORY, handle);
- else if (strnicmp(DIRECTORY.name, "SEGS", 4) == 0)
- LoadSegs(&DIRECTORY, handle);
- else if (strnicmp(DIRECTORY.name, "SSECTORS", 8) == 0)
- LoadSSectors(&DIRECTORY, handle);
- else if (strnicmp(DIRECTORY.name, "NODES", 5) == 0)
- LoadNodes(&DIRECTORY, handle);
- else if (strnicmp(DIRECTORY.name, "SECTORS", 7) == 0)
- LoadSectors(&DIRECTORY, handle);
- else
- printf("Skipping %8s.\n",DIRECTORY.name);
-
- EntriesRead++;
- EntryOffset += sizeof(Directory_Entry);
- }
- printf("Allocated memory for arrays...\n");
-
- fclose(file);
- InitTrig();
- }
-
- /* All of the following procedures read in the indicated
- * resource from a WAD file pointed to by handle.
- */
-
- #ifndef C
- void View::LoadThings( Directory_Entry *Dir, FILE *handle )
- #else
- void LoadThings(Directory_Entry *Dir, FILE *handle)
- #endif
- {
- float angle;
- #ifdef C
- short i;
- thing *Thing_Array;
- #endif
- short x, y, h, a;
-
- NumThings = (short)(Dir->size / sizeof(thing));
- printf("THINGS : %d\n", NumThings);
-
- #ifndef C
- thing *Thing_Array = new thing[NumThings];
- #else
- if ((Thing_Array = malloc(Dir->size)) == NULL)
- exit(-1);
- #endif
-
- fseek(handle, Dir->foffset, SEEK_SET);
- fread(Thing_Array, (unsigned int)Dir->size, 1L, handle);
-
- #ifndef C
- for(short i = 0;i < NumThings;i++) {
- #else
- for(i = 0;i < NumThings;i++) {
- #endif
- fix_w(Thing_Array[i].x);
- fix_w(Thing_Array[i].y);
- fix_w(Thing_Array[i].angle);
- fix_w(Thing_Array[i].thing_type);
- fix_w(Thing_Array[i].attributes);
- if (Thing_Array[i].thing_type == 1) {
- angle = (float) (182.0444444444444 * (float)Thing_Array[i].angle);
- x = Thing_Array[i].x; /* Indirection is needed in another place */
- y = Thing_Array[i].y;
- h = 40;
- a = angle;
- SetView(&x, &y, &h, &a);
- break;
- }
- }
- #ifndef C
- delete [] Thing_Array;
- #else
- free(Thing_Array);
- #endif
- }
-
- #ifndef C
- void View::LoadSegs(Directory_Entry *Dir, FILE *handle)
- #else
- void LoadSegs(Directory_Entry *Dir, FILE *handle)
- #endif
- {
- #ifdef C
- int i;
- #endif
-
- NumSegs = (short)(Dir->size / sizeof(seg));
- printf("SEGS : %d\n", NumSegs);
-
- #ifndef C
- Seg_Array = new seg[NumSegs];
- #else
- if ((Seg_Array = malloc(Dir->size)) == NULL)
- exit(-1);
- #endif
-
- fseek(handle, Dir->foffset, SEEK_SET);
- fread(Seg_Array, (unsigned int) Dir->size, 1L, handle);
-
- #ifndef C
- for(int i = 0;i < NumSegs;i++) {
- #else
- for(i = 0;i < NumSegs;i++) {
- #endif
- fix_w(Seg_Array[i].from);
- fix_w(Seg_Array[i].to);
- fix_w(Seg_Array[i].angle);
- #ifdef __GNUC__
- fix_w(Seg_Array[i].line);
- #else
- fix_w(Seg_Array[i].xline);
- #endif
- fix_w(Seg_Array[i].line_side);
- fix_w(Seg_Array[i].line_offset);
- }
- }
-
- #ifndef C
- void View::LoadSides( Directory_Entry *Dir, FILE *handle )
- #else
- void LoadSides(Directory_Entry *Dir, FILE *handle)
- #endif
- {
- #ifdef C
- int i;
- #endif
-
- NumSides = (short)(Dir->size / sizeof(side));
- printf("SIDEDEFS : %d\n", NumSides);
-
- #ifndef C
- Side_Array = new side[NumSides];
- #else
- if ((Side_Array = malloc(Dir->size)) == NULL)
- exit(-1);
- #endif
-
- fseek(handle, Dir->foffset, SEEK_SET);
- fread(Side_Array, (unsigned int)Dir->size, 1L, handle);
-
- #ifndef C
- for(int i = 0;i < NumSides;i++) {
- #else
- for(i = 0;i < NumSides;i++) {
- #endif
- fix_w(Side_Array[i].tm_xoffset);
- fix_w(Side_Array[i].tm_yoffset);
- fix_w(Side_Array[i].sector);
- }
- }
-
- #ifndef C
- void View::LoadLines( Directory_Entry *Dir, FILE *handle )
- #else
- void LoadLines(Directory_Entry *Dir, FILE *handle)
- #endif
- {
- #ifdef C
- int i;
- #endif
-
- NumLines = (short)(Dir->size / sizeof(line));
- printf("LINEDEFS : %d\n", NumLines);
-
- #ifndef C
- Line_Array = new line[NumLines];
- #else
- if ((Line_Array = malloc(Dir->size)) == NULL)
- exit(-1);
- #endif
-
- fseek(handle, Dir->foffset, SEEK_SET);
- fread(Line_Array, (unsigned int)Dir->size, 1L, handle);
-
- #ifndef C
- for(int i = 0;i < NumLines;i++) {
- #else
- for(i = 0;i < NumLines;i++) {
- #endif
- fix_w(Line_Array[i].from);
- fix_w(Line_Array[i].to);
- fix_w(Line_Array[i].flags);
- fix_w(Line_Array[i].special);
- fix_w(Line_Array[i].tag);
- fix_w(Line_Array[i].side[0]);
- fix_w(Line_Array[i].side[1]);
- }
- }
-
- #ifndef C
- void View::LoadNodes( Directory_Entry *Dir, FILE *handle )
- #else
- void LoadNodes(Directory_Entry *Dir, FILE *handle)
- #endif
- {
- #ifdef C
- int i;
- #endif
- NumNodes = (short)(Dir->size / sizeof(node));
- printf("NODES : %d\n", NumNodes);
-
- #ifndef C
- Node_Array = new node[NumNodes];
- PNode_Array = new node * [NumNodes];
- #else
- if ((Node_Array = malloc(Dir->size)) == NULL)
- exit(-1);
- if ((PNode_Array = malloc(NumNodes * sizeof(node *))) == NULL)
- exit(-1);
- #endif
- MaxNode = NumNodes - 1;
-
- fseek(handle, Dir->foffset, SEEK_SET);
- fread(Node_Array, (unsigned int)Dir->size, 1L, handle);
-
- #ifndef C
- for(int i = 0;i < NumNodes;i++) {
- #else
- for(i = 0;i < NumNodes;i++) {
- #endif
- fix_w(Node_Array[i].x);
- fix_w(Node_Array[i].y);
- fix_w(Node_Array[i].dx);
- fix_w(Node_Array[i].dy);
- fix_w(Node_Array[i].ry2);
- fix_w(Node_Array[i].ry1);
- fix_w(Node_Array[i].rx1);
- fix_w(Node_Array[i].rx2);
- fix_w(Node_Array[i].ly2);
- fix_w(Node_Array[i].ly1);
- fix_w(Node_Array[i].lx1);
- fix_w(Node_Array[i].lx2);
- fix_w(Node_Array[i].right);
- fix_w(Node_Array[i].left);
- PNode_Array[i] = &Node_Array[i];
- }
- }
-
- #ifndef C
- void View::LoadSectors(Directory_Entry *Dir, FILE *handle)
- #else
- void LoadSectors(Directory_Entry *Dir, FILE *handle)
- #endif
- {
- #ifdef C
- int i;
- #endif
- NumSectors = (short)(Dir->size / sizeof(sector));
- printf("SECTORS : %d\n", NumSectors);
-
- #ifndef C
- Sector_Array = new sector[NumSectors];
- #else
- if ((Sector_Array = malloc(Dir->size)) == NULL)
- exit(-1);
- #endif
-
- fseek(handle, Dir->foffset, SEEK_SET);
- fread(Sector_Array, (unsigned int)Dir->size, 1L, handle);
-
- #ifndef C
- for(int i = 0;i < NumSectors;i++) {
- #else
- for(i = 0;i < NumSectors;i++) {
- #endif
- fix_w(Sector_Array[i].floor_ht);
- fix_w(Sector_Array[i].ceiling_ht);
- fix_w(Sector_Array[i].light);
- fix_w(Sector_Array[i].type);
- fix_w(Sector_Array[i].trigger);
- }
- }
-
- #ifndef C
- void View::LoadVertexes(Directory_Entry *Dir, FILE *handle)
- #else
- void LoadVertexes(Directory_Entry *Dir, FILE *handle)
- #endif
- {
- #ifdef C
- int i;
- #endif
-
- NumVertexes = (short)(Dir->size / sizeof(vertex));
- printf("VERTEXES : %d\n", NumVertexes);
-
- #ifndef C
- Vertex_Array = new vertex[NumVertexes];
- #else
- if ((Vertex_Array = malloc(Dir->size)) == NULL)
- exit(-1);
- #endif
-
- fseek(handle, Dir->foffset, SEEK_SET);
- fread(Vertex_Array, (unsigned int)Dir->size, 1L, handle);
-
- #ifndef C
- for(int i = 0;i < NumVertexes;i++) {
- #else
- for(i = 0;i < NumVertexes;i++) {
- #endif
- fix_w(Vertex_Array[i].x);
- fix_w(Vertex_Array[i].y);
- }
- }
-
- #ifndef C
- void View::LoadSSectors(Directory_Entry *Dir, FILE *handle)
- #else
- void LoadSSectors(Directory_Entry *Dir, FILE *handle)
- #endif
- {
- #ifdef C
- int i;
- #endif
-
- NumSSectors = (short)(Dir->size / sizeof(ssector));
- printf("SSECTORS : %d\n", NumSSectors);
-
- #ifndef C
- SSector_Array = new ssector[NumSSectors];
- #else
- if ((SSector_Array = malloc(Dir->size)) == NULL)
- exit(-1);
- #endif
-
- fseek(handle, Dir->foffset, SEEK_SET);
- fread(SSector_Array, (unsigned int)Dir->size, 1L, handle);
-
- #ifndef C
- for(int i = 0;i < NumSSectors;i++) {
- #else
- for(i = 0;i < NumSSectors;i++) {
- #endif
- fix_w(SSector_Array[i].num_segs);
- fix_w(SSector_Array[i].first_seg);
- }
- }
-
- /* Returns the screen mode to normal text. */
- #ifndef C
- void View::Close(void)
- #else
- void Close(void)
- #endif
- {
- }
-